package com.szeldon.pj.jps.envs;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.szeldon.pj.jps.qres.AbstractQueryResult;
import com.szeldon.pj.jps.qres.BagResult;

public class ENVSStackFrame {

	private List<ENVSBinder> elements = new LinkedList<ENVSBinder>();

	public ENVSStackFrame() {

	}

	public ENVSStackFrame(List<ENVSBinder> bindersList) {
		this.elements = bindersList;
	}

	/**
	 * 
	 * @param frame
	 */
	public void add(ENVSBinder envsBinder) {
		elements.add(envsBinder);
	}

	/**
	 * Returns bag of results for given name. Null if there are no matching results.
	 * 
	 * @param name
	 * @return
	 */
	public BagResult findElementsByName(String name) {
		Set<AbstractQueryResult> setBindedResults = new HashSet<AbstractQueryResult>();
		for (int i = 0; i < elements.size(); i++) {
			ENVSBinder currentBinder = elements.get(i);
			if (currentBinder.getName().equals(name)) {
				setBindedResults.add(currentBinder.getValue());
			}
		}

		if (setBindedResults.size() == 0) {
			return null;
		} else {
			return new BagResult(setBindedResults);
		}
	}

	@Override
	public String toString() {
		return "[" + elements + "]";
	}

}
